/**
 * 产品管理js
 */
var productInfo = {
	curPage : 1,
	pageSize : 10,
	dataCache : null,
	fundType:null,

	startEvent : function() {
		// 按回车键登录事件
		document.onkeydown = function(e) {
			var ev = document.all ? window.event : e;
			if (ev.keyCode == 13) {
				productInfo.curPage = 1;
				productInfo.setPageChange(null);
				productInfo.search();
			}
		};
		$('.subType').click(function() {
			
			var fundType = $(this).attr("id");
			productInfo.fundType = fundType;
			productInfo.setPageChange(productInfo.curPage); 
			 
		    
		});
	},
	
	bindEvent : function() {
		// 查询
		$('#searchData').click(function() {
			productInfo.curPage = 1;
			productInfo.setPageChange(null);
			productInfo.search();
		});

		$('#addData').on('click', function() {
			var path = APP_ADDRESS + 'mgr/product/productType01/productInfoAdd.jsp';
			art.dialog.open(path, {
				title : '添加产品',
				width : 900,
				height : 500,
				ok : function() {
					var returnStatus = this.iframe.contentWindow.submitForm();
					if (returnStatus) {
						productInfo.setPageChange(null);
						productInfo.search();
					}
					return false;
				},
				cancel : true
			});
		});
		
			
		
	},

	// 设置分页
	setPageChange : function(delCurPage) {
		var url = '/fundMessageList';
		
		var params = {
			'curPage' : productInfo.curPage,
			'pageSize' : productInfo.pageSize,
			'fundType' :productInfo.fundType,
			'onSale' :$.trim($('#onSale option:selected').val()),
			'fundCode' : $.trim($('#searchModForm input[name="fundCode"]').val()),
			'fundFullName' : $.trim($('#searchModForm input[name="fundFullName"]').val())
		};
		var cb = function(data) {
			if (data.resultCode == 200) {
				var totalCount = data.size;
				var pageTotal = (totalCount / productInfo.pageSize > 0) ? Math
						.ceil(totalCount / productInfo.pageSize) : 1;
				pageHelper.init(pageTotal, totalCount, productInfo.search,
						delCurPage); // 翻页事件
			}
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},

	// 查询基金
	search : function(pageIdx) {
		var url = '/fundMessageList';;
		if (typeof (pageIdx) != "undefined") {
			productInfo.curPage = pageIdx;   
		}
		 
		var params = {
				'curPage' : productInfo.curPage,
				'pageSize' : productInfo.pageSize,
				'fundType' :productInfo.fundType,
				'onSale' :$.trim($('#onSale option:selected').val()),//$('#onSale option:selected') .val();
				'fundCode' : $.trim($('#searchModForm input[name="fundCode"]').val()),
				'fundFullName' : $.trim($('#searchModForm input[name="fundFullName"]').val())
		};
		var cb = function(data) {
			if (data.resultCode == 200) {
				$('#tableList tbody').empty();
				var tr = '';
				if (data.result != null && data.result.length > 0) {
					for (var i = 0; i < data.result.length; i++) {
						var singleObj = data.result[i];
						
						var setupTime = "";
						if(singleObj.fundInfo.setupDate != null){
							setupTime =getSmpFormatDateByLong(singleObj.fundInfo.createdts,true);
						}
										
						tr += '<tr><td><input type="hidden" name="fundCode" value="'
								+ singleObj.fundInfo.fundCode
								+ '"/>'
								+ (i + 1)
								+ '</td><td>'
								+ singleObj.fundInfo.fundCode
								+ '</td><td>'
								+ singleObj.fundInfo.fundFullName
								+ '</td><td>'
								+ getFundType(singleObj.fundInfo.fundType)
								+ '</td><td>'
								+ singleObj.fundInfo.trustee
								+ '</td><td>'
								+ setupTime
								+ '</td><td>'
								+ singleObj.fundInfo.minIndividualSubscribeAmount
								+ '</td><td>'
								+ "T+"+singleObj.fundInfo.refundPace+"日"
								+ '</td><td>'
								+ '<a type="button" name="productType01_9999" class="btn btn-primary btn-xs lookData" style="margin-left: 5px;display:none;">'
								+ '<i class="fa fa-reorder"></i> 查看 </a>'
								+ '</td></tr>';
					}
				} else {
					tr = '<tr><td colspan="7" style="text-align: center;color: red;">暂无数据</td></tr>';
				}
				$('#tableList tbody').html(tr);
				productInfo.bindEventOtherBtn();
				jsUtils.controlFunctionBtn();
			}
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},

	bindEventOtherBtn : function() {
		$('.deleteData').unbind();
		$('.deleteData').on(
				'click',
				function() {
					var thisObj = $(this);
					art.dialog.confirm('你确认删除？', function() {
						var id = thisObj.parent().parent().find(
								'input[name="id"]').eq(0).val();
						var url = 'mgr/ProductInfoCtrl/deleteProductInfo';
						var params = {
							'id' : id
						};
						var cb = function(data) {
							alert('删除成功！');
							productInfo.setPageChange(productInfo.curPage);
							productInfo.search(productInfo.curPage);
						}
						sendApiNoLoad(url, params, cb, 'POST');
					}, function() {
					});
				});
		$('.updateData').unbind();
		$('.updateData').on(
				'click',
				function() {
					var thisObj = $(this);
					var id = thisObj.parent().parent().find('input[name="id"]')
							.eq(0).val();
					art.dialog.data('updId', id);
					var path = APP_ADDRESS
							+ 'mgr/hkproduct/productInfoUpdate.jsp';
					art.dialog.open(path, {
						title : '修改产品',
						width : 900,
						height : 500,
						ok : function() {
							var returnStatus = this.iframe.contentWindow
									.submitForm();
							if (returnStatus) {
								productInfo.setPageChange(productInfo.curPage);
								productInfo.search(productInfo.curPage);
							}
							return false;
						},
						cancel : true
					});
				});
		$('.bespeak').unbind();
		$('.bespeak').on(
				'click',
				function() {
					var thisObj = $(this);
					var id = thisObj.parent().parent().find('input[name="id"]')
							.eq(0).val();
					
					var productNo = thisObj.parent().parent().find('input[name="productNo"]')
					.eq(0).val();
					
					var productName = thisObj.parent().parent().find('input[name="productName"]')
					.eq(0).val();
					
					
					art.dialog.data('productId', id);
					art.dialog.data('productCode', productNo);
					art.dialog.data('productName', productName);
					
					var path = APP_ADDRESS
							+ 'mgr/product/bespeakAdd.jsp';
					art.dialog.open(path, {
						title : '产品预约',
						width : 500,
						height : 200,
						ok : function() {
							var returnStatus = this.iframe.contentWindow
									.submitForm();
							if (returnStatus) {
								productInfo.setPageChange(productInfo.curPage);
								productInfo.search(productInfo.curPage);
							}
							return false;
						},
						cancel : true
					});
				});
		$('.lookData').unbind();
		$('.lookData').on(
				'click',
				function() {
					var thisObj = $(this);
					var id = thisObj.parent().parent().find('input[name="id"]')
							.eq(0).val();
					art.dialog.data('lokId', id);
					var path = APP_ADDRESS
							+ 'mgr/hkproduct/productInfoLook.jsp';
					art.dialog.open(path, {
						title : '查看产品',
						width : 900,
						height : 500,
						cancel : true
					});
				});
	},

	init : function() {
		productInfo.fundType = null;
		productInfo.setPageChange(null);
		productInfo.search();
		productInfo.startEvent();
		productInfo.bindEvent();
		jsUtils.controlFunctionBtn();
	}
};

$(document).ready(function() {
	productInfo.init();
});

//提供给外部刷新调用
function refashData() {
	productInfo.setPageChange(null);
	productInfo.search();
};
